package com.ktjy.controller;

import com.ktjy.pojo.SysUser;
import com.ktjy.service.ISysUserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author 黄军艳
 * @version 1.0
 * @date 2025/1/9  11:45
 */
@Controller
public class LoginController {
    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    @RequestMapping("/main")
    public String main() {
        return "main";
    }

    @Resource
    ISysUserService sysUserService;

    @RequestMapping(value="/dologin")
    public String dologin(String usrName, String usrPassword,
                          HttpSession session, HttpServletRequest request){
        //调用service方法，进行用户匹配
        SysUser sysUser = sysUserService.login(usrName, usrPassword);
        if(sysUser != null){//登录成功
            //放入session
            session.setAttribute("loginUser", sysUser);
            //重定向到main方法
            return "main";
        }else{
            //页面跳转（login.html）带提示信息--转发
            request.setAttribute("message", "用户名或密码不正确");
            //转发到login.html
            return "login";
        }
    }

    /**
     * 退出
     * @param session
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("loginUser");
        return "login";
    }
}
